package com.drore.logapp;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.drore.app.LogRecordUtil;
import com.drore.app.utils.EventType;
import com.drore.logapp.base.BaseActivity;
import com.drore.logapp.ui.NetWorkActivity;
import com.drore.www.logbeansecret.bean.EventBean;
import com.drore.www.logbeansecret.bean.NetConnectBean;
import com.drore.www.logbeansecret.bean.PhoneBean;

import java.util.List;


public class MainActivity extends BaseActivity implements View.OnClickListener {
    private TextView textView;
    private String TAG = "MainActivity";

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        findViewById(R.id.addclick).setOnClickListener(this);
        findViewById(R.id.commit).setOnClickListener(this);
        findViewById(R.id.bug2).setOnClickListener(this);
        findViewById(R.id.jump).setOnClickListener(this);
        findViewById(R.id.bug).setOnClickListener(this);
        findViewById(R.id.delete).setOnClickListener(this);
        textView = findViewById(R.id.result);
        requestPermission();
        LogRecordUtil.setAppLaunchEvent();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogRecordUtil.setAppStopEvent();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bug:
                LogRecordUtil.setBugEvent("add bug 我是一个假bug的内容！");
                commit();
                break;
            case R.id.addclick:
                LogRecordUtil.setClickEvent("add click");
                commit();
                break;
            case R.id.bug2:
                String s = "23SD";
                Integer.parseInt(s);
                break;
            case R.id.jump:
                startActivity(new Intent(this, NetWorkActivity.class));
                break;
            case R.id.commit:
                commit();
                LogRecordUtil.sendLogs();
                break;
            case R.id.delete:
                LogRecordUtil.deleteAllEventAndPhoneInfo();
                commit();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        commit();
    }

    private void commit() {
        List<EventBean> listEvent = LogRecordUtil.getEventBeanList();
        List<PhoneBean> phoneBeanList = LogRecordUtil.getPhoneBeanList();
        List<NetConnectBean> netConnectBeanList = LogRecordUtil.getNetSatauBeanList();
        textView.setText("手机信息数量：" + phoneBeanList.size()
                + "\n事件信息数量：" + listEvent.size()
                + "\n请求信息数量：" + netConnectBeanList.size());
        for (EventBean eventBean : listEvent) {
            if (eventBean.getEventtype().equals("" + EventType.BUG_EVENT)) {
                String BUG = eventBean.getEventdetail();
                Log.e(TAG,  BUG);
            }
        }
    }

    private static final int REQUEST_PERMISSION = 0;

    private void requestPermission() {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE},
                REQUEST_PERMISSION);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == REQUEST_PERMISSION) {
            if ((grantResults.length == 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                    && grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
            } else {
                Log.e(TAG, "We highly recommend that you need to grant the special permissions before initializing the SDK, otherwise some "
                        + "functions will not work");
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}
